/**
 * Created by shidaiapp on 2017/9/29.
 */
import actionUrls from "../../../../api/ApiUrls"
import {createDownloadForm} from "../../../js/downloadUtils"
import tiyData from "./order_cuorier_data";
import Vue from "vue";


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalDeliver.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDeliver.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.orderState = '';
  tiyData.tParams.orderSerialNumber = '';
  tiyData.tData.orderItemDict = self.orderItemDict;
  self.$ySpliceAll(tiyData.tData.orderCuorierList);
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.orderCuorierList);
  let data = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "serialNumber": tiyData.tParams.orderSerialNumber,
    "state": tiyData.tParams.orderState,
    "orderBy": tiyData.tParams.orderBy,

  }

  self.$apiOrder.getOrderCuorierList(data).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let orderPage = response.result;
      tiyData.tPage.total = orderPage.total;
      tiyData.tPage.stepNumber = orderPage.pageSize;
      orderPage.list.forEach(
        function (item) {
          if (item.state < 110 || item.state > 116) {
            item._disabled = true;
          }
          tiyData.tData.orderCuorierList.push(item);
        }
      );
      self.params.selectedOrderItems.splice(0, self.params.selectedOrderItems.length);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};




/**
 * 清空已经选中的订单
 * @param self
 * @param tiyData
 */
export const clearSelectedOrders = (self, tiyData) => {
  self.$refs['orderCuorierList'].selectAll(false);
  tiyData.tParams.selectedOrders = [];
}




/**修改重要说明
 * @param self
 * @param params {orderIds:[], importantNote:""}
 * */
export const modifyImportantNote = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOrder.modifyImportantNote(tiyData.tParams.modifyImportantNote).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('修改重要说明成功!');
      tiyData.tStatus.modalImportantNote.isShow = false;
      webList(self, tiyData);
    } else {
      let error = "修改重要说明失败！";
      if(response.errorMessage){
        error += response.errorMessage;
      }
      self.$Message.error(error);
    }
  });
};
